package com.jiyinit.web.core.mobile.controller;

import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.base.util.JsonEntityTransform;
import com.jiyinit.core.framework.base.util.LogUtil;
import com.jiyinit.web.core.business.dto.NoticePageRequest;
import com.jiyinit.web.core.business.dto.NoticeRequest;
import com.jiyinit.web.core.business.entity.TabCoreNoticeEntity;
import com.jiyinit.web.core.business.service.NoticeServiceImpl;
import com.jiyinit.web.core.business.vo.NoticeUserVo;
import com.jiyinit.web.core.business.vo.NoticeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lr on 2017/12/29.
 */
@Controller
@RequestMapping("/mobile/notice")
public class MnoticeController {
    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(MnoticeController.class);

    @Resource(name = "core.noticeServiceImpl")
    private NoticeServiceImpl  noticeService;

    /**
     * 手机端新增通知接口
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "add")
    public ResponseEntity<ResponseDto> add(HttpServletRequest httpServletRequest){
        NoticeRequest noticeRequest = getJsonToTaskRequest(httpServletRequest);
        if (noticeRequest.getTitle() == null || "".equals(noticeRequest.getTitle())) {
            return new ResponseEntity<>(ResponseDto.responseFail("标题不能为空"), HttpStatus.OK);
        }
        if (noticeRequest.getContent() == null || "".equals(noticeRequest.getContent())) {
            return new ResponseEntity<>(ResponseDto.responseFail("内容不能为空"), HttpStatus.OK);
        }
        if (noticeRequest.getPriority() == null ) {
            return new ResponseEntity<>(ResponseDto.responseFail("优先级不能为空"), HttpStatus.OK);
        }
        if (noticeRequest.getChannel() == null || "".equals(noticeRequest.getChannel())) {
            return new ResponseEntity<>(ResponseDto.responseFail("渠道不能为空"), HttpStatus.OK);
        }

        int num = noticeService.add(noticeRequest);
        if(num > 0)
            return new ResponseEntity<>( ResponseDto.responseOK(""), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("新建失败！"), HttpStatus.OK);
    }

    /**
     * 手机端修改通知接口(由于添加时已经推送给相关人员了，所以修改时，人员不可修改)
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "update")
    public ResponseEntity<ResponseDto> update(HttpServletRequest httpServletRequest){
        NoticeRequest noticeRequest = getJsonToTaskRequest(httpServletRequest);
        if (noticeRequest.getNoticeId() == null ) {
            return new ResponseEntity<>(ResponseDto.responseFail("通知Id不能为空"), HttpStatus.OK);
        }
        //只能本人发出的通知
        TabCoreNoticeEntity entity = noticeService.findById(noticeRequest.getNoticeId());
        if(entity.getCreateId().intValue() != noticeRequest.getUserId().intValue())
            return new ResponseEntity<>( ResponseDto.responseFail("没有权限修改"), HttpStatus.OK);

        int num = noticeService.update(noticeRequest);
        if(num > 0)
            return new ResponseEntity<>( ResponseDto.responseOK(""), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("修改失败！"), HttpStatus.OK);
    }

    /**
     * 通知详情
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "detail")
    public ResponseEntity<ResponseDto> detail(HttpServletRequest httpServletRequest){
        NoticeRequest noticeRequest = getJsonToTaskRequest(httpServletRequest);
        if (noticeRequest.getNoticeId() == null ) {
            return new ResponseEntity<>(ResponseDto.responseFail("通知Id不能为空"), HttpStatus.OK);
        }
        NoticeVo notice = noticeService.detail(noticeRequest.getNoticeId());
        List<NoticeUserVo> list = noticeService.findAlloters(noticeRequest.getNoticeId());
        HashMap<String,Object> result =  new HashMap<String,Object>();
        result.put("notice",notice);
        result.put("alloters",list);

        return new ResponseEntity<>( ResponseDto.responseOK(result), HttpStatus.OK);
    }

    /**
     * 通知删除
     * @param httpServletRequest
     * @return
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "delete")
    public ResponseEntity<ResponseDto> delete(HttpServletRequest httpServletRequest){
        NoticeRequest noticeRequest = getJsonToTaskRequest(httpServletRequest);
        if (noticeRequest.getNoticeId() == null ) {
            return new ResponseEntity<>(ResponseDto.responseFail("通知Id不能为空"), HttpStatus.OK);
        }

        //只能本人发出的通知
        TabCoreNoticeEntity entity = noticeService.findById(noticeRequest.getNoticeId());
        if(entity.getCreateId().intValue() != noticeRequest.getUserId().intValue())
            return new ResponseEntity<>( ResponseDto.responseFail("没有权限删除"), HttpStatus.OK);

        int  num = noticeService.delete(noticeRequest.getNoticeId(),noticeRequest.getUserId());
        if(num > 0)
            return new ResponseEntity<>( ResponseDto.responseOK(""), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("删除失败！"), HttpStatus.OK);
    }

    /**
     * 全部
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("findByPage")
    public ResponseEntity<ResponseDto> findByPage(HttpServletRequest request){
        NoticeRequest noticeRequest = this.getJsonToTaskRequest(request);
        PagingResult<NoticeVo> pageingResult=noticeService.findByPageForMobile(noticeRequest);
        return new ResponseEntity<>(ResponseDto.responseOK(pageingResult), HttpStatus.OK);
    }

    /**
     * 我发布的通知
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("findPublishByPage")
    public ResponseEntity<ResponseDto> findPublishByPage(HttpServletRequest request){
        NoticeRequest noticeRequest = this.getJsonToTaskRequest(request);
        PagingResult<NoticeVo> pageingResult=noticeService.findPublishByPage(noticeRequest);
        return new ResponseEntity<>(ResponseDto.responseOK(pageingResult), HttpStatus.OK);
    }

    /**
     * 我收到的通知
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("findReceviceByPage")
    public ResponseEntity<ResponseDto> findReceviceByPage(HttpServletRequest request){
        NoticeRequest noticeRequest = this.getJsonToTaskRequest(request);
        PagingResult<NoticeVo> pageingResult=noticeService.findReceviceByPage(noticeRequest);
        return new ResponseEntity<>(ResponseDto.responseOK(pageingResult), HttpStatus.OK);
    }


    private NoticeRequest getJsonToTaskRequest(HttpServletRequest request){
        String content = (String) request.getAttribute("request_json");
        String logJson = request.getParameter("request_json");
        LogUtil.info(LOGGER, "logJson:{}", logJson);
        NoticeRequest noticeRequest = (NoticeRequest) JsonEntityTransform.json2Object(logJson, NoticeRequest.class);
        return noticeRequest;
    }



    /**
     * 手机端文件上传
     * @param httpServletRequest
     * @return 2019/1/16
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "addFile")
    public ResponseEntity<ResponseDto> addFile(HttpServletRequest httpServletRequest){
        NoticeRequest noticeRequest = getJsonToTaskRequest(httpServletRequest);
        //url在文件上传成功后返回给页面，在传递过来
        if (noticeRequest.getTitle() == null || "".equals(noticeRequest.getTitle())) {
            return new ResponseEntity<>(ResponseDto.responseFail("标题不能为空"), HttpStatus.OK);
        }
        if (noticeRequest.getContent() == null || "".equals(noticeRequest.getContent())) {
            return new ResponseEntity<>(ResponseDto.responseFail("内容不能为空"), HttpStatus.OK);
        }
        if (noticeRequest.getPriority() == null ) {
            return new ResponseEntity<>(ResponseDto.responseFail("优先级不能为空"), HttpStatus.OK);
        }
        if (noticeRequest.getChannel() == null || "".equals(noticeRequest.getChannel())) {
            return new ResponseEntity<>(ResponseDto.responseFail("渠道不能为空"), HttpStatus.OK);
        }

        int num = noticeService.addNotice(noticeRequest);
        if(num > 0)
            return new ResponseEntity<>( ResponseDto.responseOK(""), HttpStatus.OK);
        return new ResponseEntity<>( ResponseDto.responseFail("新建失败！"), HttpStatus.OK);
    }



}
